#include "hbase_client.h"
#include "json/json.h"
#include "base.h"
#include "datainit.h"

struct RowResult *parse_json(const char *storage, const char *rowkey, const char *json_data, int datalen)
{
    Json::Reader reader;
    Json::Value value;
	std::string rkey;
	int isnotindex = (strcmp(storage, "index") != 0);
	char prefix[10];
	snprintf(prefix, 10, "%s:", COL_CLUSTER);

    struct RowResult *table = NULL;

	if (json_data == NULL || *json_data == '\0' || datalen <= 0) {
		DERROR("json string is null\n");
		return NULL;
	}

    if (reader.parse(json_data, value)) {
        Json::Value::Members keys;
		if (isnotindex) {
			rkey = rowkey;
		}

        if (value.size() <= 0) {
			DERROR("json parse error: json string is invalid, json data: %s\n", json_data);
            return NULL;
        } else {
			size_t i;
			size_t j;
			Json::Value array = value["array"];

			for (i = 0; i < array.size(); i++) {
				struct RowResult *row = new (struct RowResult);
				row->table = storage;
				row->next = table;
				table = row;

				std::string pref;
				std::string subf;
				
				Json::Value keyobj = array[i];
				keys = keyobj.getMemberNames();
				for (j = 0; j < keys.size(); j++) {
					//char key[1024];
					//snprintf(key, 1024, "%s", keys[j].c_str());
					if (isnotindex) {
						if (rkey == keys[j]) {
							row->row = keyobj[keys[j]].asString();
						}
					} else {
						if (keys[j] == "storage")
							pref = keyobj[keys[j]].asString();
						if (keys[j] == "index")
							subf = keyobj[keys[j]].asString();
					}
					row->insert(prefix+keys[j], array[i][keys[j]].asString(), 0);
					//DERROR("%s->%s\n", (prefix+keys[j]).c_str(), array[i][keys[j]].asString().c_str());
				}
				if (!isnotindex) {
					row->row = pref + "_" + subf;
				}
			}
			return table;
		}
    } else {
		DERROR("json error: json cpp cant parse json: %s\n", json_data);
		return NULL;
	}
}

void get_json(const DIR_INFO& info, Json::Value& item)
{
    item.clear();
    
    item["userid"] = (int)info._parentID._parts._userID;
    item["parentid"] = (int)info._parentID._parts._dirSN;
    item["dirid"] = (int)info._dirID._parts._dirSN;
    item["name"] = info._name;
    item["createtime"] = info._createTime;
    item["updatetime"] = info._updateTime;
    item["deletetime"] = info._deleteTime;
    item["isdeleted"] = info._isDeleted;
    item["filecount"] = info._fileCount;
    item["totalsize"] = info._totalSize;
    item["type"] = "DIR";
}

void get_json(const FILE_INFO& info, Json::Value& item)
{
    item.clear();
    
    item["userid"] = (int)info._parentID._parts._userID;
    item["parentid"] = (int)info._parentID._parts._dirSN;
    item["name"] = info._name;
    item["version"] = info._fileVersion;
    item["length"] = (int)info._length;
    item["createtime"] = info._createTime;
    item["deletetime"] = info._deleteTime;
    item["isdeleted"] = info._isDeleted;
    item["compresstype"] = info._compressType;
    item["securityType"] = info._securityType;
    item["headcrc"] = info._headCRC;
    item["datacrc"] = info._dataCRC;
    item["type"] = "FILE";
}
